const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appletMobile: '', //获取手机号
    codeHtml: "获取验证码", //获取验证码按钮 
    codeDisabled: true,
    validCode: '', //验证码
    disabled: true, //下一步
    hasPwd: '0', //0未设置密码，1已设置密码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    that.setData({
      hasPwd:options.pwdType
    },function(){
      that.getMobile()
    })
  },

  /**
   * 查询用户手机号
   */
  getMobile: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
    }
    giftShopModel.userPwdMobile(param).then(data => {
      this.setData({
        appletMobile: data.appletMobile,
        codeDisabled: false,
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 发短信验证码
   */
  getCode: function () {
    wx.reportAnalytics('gift_password_getcode');
    this.setData({
      codeDisabled: true,
      validCode: '', //验证码
    })
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
    }
    giftShopModel.userPwdSendMsg(param).then(data => {
      this.startReportHeart(); //倒计时
    }).catch(err => {
      this.setData({
        codeDisabled: false,
      })
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 检测验证码的合法性
   */
  setValidCode: function (e) {
    var value = e.detail.value;
    let that = this;
    if (value != '') {
      that.setData({
        disabled: false, //下一步
      })
    } else {
      that.setData({
        disabled: true, //下一步
      })
    }
    that.setData({
      validCode: value,
    })
  },

  /**
   * 下一步
   */
  setNext: function () {
    wx.reportAnalytics('gift_password_next');
    this.setData({
      disabled: true, //下一步
    })
    if (this.data.validCode.length == 6) {
      this.getUserPwdMobileAuth();
    } else {
      wx.showToast({
        title: `验证码输入错误`,
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 验证短信验证码
   */
  getUserPwdMobileAuth: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      valiCode: this.data.validCode, //短信验证码
    }
    giftShopModel.userPwdMobileAuth(param).then(data => {
      wx.navigateTo({
        url: '/pages/giftShop/password/paymentCode/index?hasPwd='+this.data.hasPwd
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /** 
   * 计时器 
   */
  startReportHeart: function () {
    var time = 60;
    var that = this;
    var timerTem = setInterval(function (e) {
      time--;
      that.setData({
        codeHtml: time + "S后重试"
      })
      if (time == 0)
        clearInterval(timerTem),
        that.setData({
          codeDisabled: false,
          codeHtml: "获取验证码"
        })
    }, 1000)
  },

})